package org.jeecg.modules.jd.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.jd.entity.JD_goods;
import org.jeecg.modules.pdd.util.DingDanXiaUtil;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JDGoodsUtil {

    /**
     * 频道id：1-好券商品,2-超级大卖场,10-9.9专区,22-热销爆品,23-为你推荐,24-数码家电,25-超市,
     * 26-母婴玩具,27-家具日用,28-美妆穿搭,29-医药保健,30-图书文具,31-今日必推,32-品牌好货,
     * 33-秒杀商品,34-拼购商品,40-高收益,41-自营热卖榜,109-新品首发,110-自营,125-首购商品,129-高佣榜单,130-视频商品
     *
     * @param jdGoodRequest
     */
    public static  Map<String,Object> getJingfenGoods(JDJingFenGoodRequest jdGoodRequest) {
        Map<String,Object> resultMap=new HashMap<>();
        String url = "http://api.tbk.dingdanxia.com/jd/query_jingfen_goods";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("eliteId", jdGoodRequest.getEliteId());
        map.put("pageIndex", jdGoodRequest.getPageIndex());
        map.put("pageSize", jdGoodRequest.getPageSize());
        //排序字段(price：单价, commissionShare：佣金比例, commission：佣金，
        // inOrderCount30DaysSku：sku维度30天引单量，comments：评论数，goodComments：好评数)
        if(null!= jdGoodRequest.getSortName()){
            map.put("sortName", jdGoodRequest.getSortName());
        }
        if(null!= jdGoodRequest.getSort()){
            map.put("sort", jdGoodRequest.getSort());
        }
        map.put("pid", JDContants.unionId + "_" + JDContants.appId + "_" + jdGoodRequest.getJd_position_id());
        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(resultStr);
        String contentlist = jsonObject.getString("data");
        Long total_results = jsonObject.getLong("total_results");
        resultMap.put("total_results",total_results);

        List<JD_goods> jd_positionList = JSON.parseArray(contentlist, JD_goods.class);
        resultMap.put("goods_data",jd_positionList);
        return resultMap;

    }

    public static  Map<String,Object> getSuperQueryList(JDJingFenGoodRequest jdGoodRequest) {
        Map<String,Object> resultMap=new HashMap<>();
        if(null==jdGoodRequest.getPageIndex()){
            jdGoodRequest.setPageIndex(1);
        }
        if(null==jdGoodRequest.getPageSize()){
            jdGoodRequest.setPageSize(30);
        }


        String url = "http://api.tbk.dingdanxia.com/jd/query_goods";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("keyword", jdGoodRequest.getKeyword());
        map.put("pageIndex", jdGoodRequest.getPageIndex());
        map.put("pageSize", jdGoodRequest.getPageSize());
        //排序字段(price：单价, commissionShare：佣金比例, commission：佣金，
        // inOrderCount30DaysSku：sku维度30天引单量，comments：评论数，goodComments：好评数)
        if(null!= jdGoodRequest.getSortName()){
            map.put("sortName", jdGoodRequest.getSortName());
        }
        if(null!= jdGoodRequest.getSort()){
            map.put("sort", jdGoodRequest.getSort());
        }
        map.put("pid", JDContants.unionId + "_" + JDContants.appId + "_" + jdGoodRequest.getJd_position_id());
        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(resultStr);
        String contentlist = jsonObject.getString("data");
        Long total_results = jsonObject.getLong("total_results");
        resultMap.put("total_results",total_results);

        List<JD_goods> jd_positionList = JSON.parseArray(contentlist, JD_goods.class);
        resultMap.put("goods_data",jd_positionList);

        return resultMap;

    }


    public static Map<String,Object> getGoods(JDGoodsRequest jdGoodsRequest) {
        Map<String,Object> resultMap=new HashMap<>();

        String url = "http://api.tbk.dingdanxia.com/jd/query_goods";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        if(null!= jdGoodsRequest.getCid1()) {
            map.put("cid1", jdGoodsRequest.getCid1());
        }
        if(null!= jdGoodsRequest.getCid2()) {
            map.put("cid2", jdGoodsRequest.getCid2());
        }
        if(null!= jdGoodsRequest.getCid3()) {
            map.put("cid3", jdGoodsRequest.getCid3());
        }
        if(null!= jdGoodsRequest.getKeyword()) {
            map.put("keyword", jdGoodsRequest.getKeyword());
        }
        map.put("pageIndex", jdGoodsRequest.getPageIndex());
        map.put("pageSize", jdGoodsRequest.getPageSize());
        map.put("isCoupon", 1);
        map.put("pid", JDContants.unionId + "_" + JDContants.appId + "_" + jdGoodsRequest.getJd_position_id());

        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(resultStr);
        String contentlist = jsonObject.getString("data");
        Long total_results = jsonObject.getLong("total_results");
        resultMap.put("total_results",total_results);

        List<JD_goods> jd_positionList = JSON.parseArray(contentlist, JD_goods.class);
        resultMap.put("goods_data",jd_positionList);

        return resultMap;

    }


    public static  String  zhuanlian(String materialId,Long positionId,String couponUrl){
        Map<String,Object> resultMap=new HashMap<>();
        String url = "http://api.tbk.dingdanxia.com/jd/by_unionid_promotion";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("materialId", materialId);
        map.put("unionId", JDContants.unionId);
        map.put("positionId", positionId);
        String resultStr = "";
        try {
        couponUrl= URLEncoder.encode(couponUrl, "utf8");
        map.put("couponUrl", couponUrl);
        map.put("chainType", 3);
            resultStr = HttpUtil.post(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return resultStr;

    }


}
